from django.shortcuts import render

# Create your views here.
from rest_framework.response import Response
from rest_framework.views import APIView

from workers.models import Department,Duty,Staff
# from workers.ser import DepartmentSerializer,DutySerializer
from workers.ser import DepartmentModelSerializer,DutyModelSerializer,StaffModelSerializer
from rest_framework.generics import ListCreateAPIView,RetrieveUpdateDestroyAPIView
# 部门增加展示
class DeparmentAPIView(ListCreateAPIView):
    queryset = Department.objects.all()
    serializer_class = DepartmentModelSerializer
#部门的单个查询、删除、修改、
class DeparmentDetailAPIView(RetrieveUpdateDestroyAPIView):
    queryset = Department.objects.all()
    serializer_class = DepartmentModelSerializer
# 职务增加展示
class DutyAPIView(ListCreateAPIView):
    queryset = Duty.objects.all()
    serializer_class = DutyModelSerializer
#职务的单个查询、删除、修改
class DutyDetialAPIView(RetrieveUpdateDestroyAPIView):
    queryset = Duty.objects.all()
    serializer_class = DutyModelSerializer
# 员工增加展示
class StaffAPIView(ListCreateAPIView):
    queryset = Staff.objects.all()
    serializer_class = StaffModelSerializer
# 员工单个查询删改
class StaffDetailAPIView(RetrieveUpdateDestroyAPIView):
    queryset = Staff.objects.all()
    serializer_class = StaffModelSerializer

# class DepartmentView(APIView):
#     # 查询全部部门
#     def get(self, request):
#         # 查询所有的数据
#         department_list = Department.objects.all()
#         # 对数据进行序列化
#         dep_serializer = DepartmentSerializer(department_list, many=True)
#         # 返回数据
#         return Response(dep_serializer.data)
#     # 添加部门
#     def post(self, request):
#         # 接收网页发送的数据
#         name = request.data.get('name')
#         # 获取部门对象，用来判断部门是否存在
#         dep = Department.objects.filter(name=name)
#         # 判断部门是否存在，如果存在，返回部门存在
#         if dep:
#             return Response({'msg': '部门存在', 'code': "400"})
#         # 不存在时，反序列化添加部门
#         dep_ser = DepartmentSerializer(data=request.data)
#         # 校验
#         if dep_ser.is_valid():
#             # 校验通过，保存数据
#             dep_ser.save()
#             # 返回
#             return Response({'msg': '添加成功', 'code': "200"})
#         # 校验失败，返回错误消息
#         else:
#             return Response({'error': dep_ser.errors, 'code': "400"})
#
# class DutyAPIView(APIView):
#     # 查询所有职责
#     def get(self, request):
#         # 查询所有的数据
#         department_list = Duty.objects.all()
#         # 对数据进行序列化
#         dep_serializer = DutySerializer(department_list, many=True)
#         # 返回数据
#         return Response(dep_serializer.data)
#     # 添加职责
#     def post(self, request):
#         # 接收网页发送的数据
#         name = request.data.get('name')
#         # 获取部门对象，用来判断部门是否存在
#         dep = Duty.objects.filter(name=name)
#         # 判断部门是否存在，如果存在，返回部门存在
#         if dep:
#             return Response({'msg': '职责存在', 'code': "400"})
#         # 不存在时，反序列化添加部门
#         dep_ser = DutySerializer(data=request.data)
#         # 校验
#         if dep_ser.is_valid():
#             # 校验通过，保存数据
#             dep_ser.save()
#             # 返回
#             return Response({'msg': '添加成功', 'code': "200"})
#         # 校验失败，返回错误消息
#         else:
#             return Response({'error': dep_ser.errors, 'code': "400"})

# class StaffAPIView(LI)

